﻿using System;

using Microsoft.Xna.Framework;

using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Common.PolygonManipulation;

namespace Spacer {
	public class Asteroid : Entity {
		public Asteroid() {}

		public Asteroid( SolarSys world ) : base(world) {
			if( this.GetType() == typeof(Asteroid) ) Initialize();
		}

		public override void Initialize() {
			base.Initialize();

			int range = Globals.GetRandomRange( 1, 3 );

			DrawMode = DrawMode.Sprite;
            DrawScale = DrawScale * .5f;
			DrawLayer = Layer.LAYER_17;
            
			Sprite = Main.Sprites["RealisticAsteroid"+range];

			DrawColor = new Color( 1f, 1f, 1f, 1f );

			PhysicsInit( 100f, MovementMode.Normal );

			Mass = 10f;
			Friction = 0.3f;
			Health = 250f * range;
			MaxHealth = 250f * range;
		}

		public override void OnTakeDamage( float amount, Entity instigator, Entity inflictor, Vector2 pos, Vector2 dir ) {
			/*if( PhysicsObject != null && (Health/MaxHealth) < 0.5f && MaxHealth >= 150f ) {
				foreach(Fixture f in PhysicsObject.FixtureList) {
					Vertices a, b;
					CuttingTools.SplitShape( f, pos, pos+5f*dir, 0.001f, out a, out b );
					
					AsteroidPiece piece_a = new AsteroidPiece( World, a );
					piece_a.Position = Position;
					
					AsteroidPiece piece_b = new AsteroidPiece( World, b );
					piece_b.Position = Position;

					Remove();
				}
			}*/
		}

        public override bool OnCollision( Entity other, FarseerPhysics.Dynamics.Contacts.Contact contact ) {
            if( other is Planet ) {
                Remove();
                return false;
            }
            return true;
        }
	}

	// ----------

	/*public class AsteroidPiece : Entity {
		public AsteroidPiece( World world, Vertices verts ) : base(world) {
			int range = Globals.GetRandomRange( 1, 3 );

			DrawMode = DrawMode.None;
			DrawColor = new Color( 1f, 1f, 1f, 1f );
			DrawOrigin = new Vector2( 0f, 0f );

			PhysicsInitEx( verts, 100f, MovementMode.Normal );
			
			Mass = 10f;
			Friction = 0.3f;
			Health = 150f * range;
			MaxHealth = 150f * range;
		}
	}*/
}
